package Day015_File类_递归_IO流.demo07_字节输入流;

import java.io.FileInputStream;

/**
 * @author zhangyan
 * @date 2020/5/6 16:01
 */
public class Test3_FileInputStream读取字节数组 {
    public static void main(String[] args) throws Exception{

        //2.读字节数组
        //创建字节输入流对象,关联数据源文件
        FileInputStream fis1 = new FileInputStream("Demo02\\aaa\\a.txt");
        //读取数据
        //创建字节数组,用来存储读取的字节数据
        byte[] bytes = new byte[3]; // 系统字节数组: 1024*8=8192  8kb

        /*

        int len1 = fis1.read(bytes); // 返回值是 读取到的字节个数
        System.out.println(len1); //3
        System.out.println(new String(bytes)); //abc

        int len2 = fis1.read(bytes);
        System.out.println(len2); //3
        System.out.println(new String(bytes)); //def

        int len3 = fis1.read(bytes);
        System.out.println(len3); // 1
        //System.out.println(new String(bytes)); // gef!!!!!!!!!!!!!!!  第一个读到g,就没了 后面2个位置还是e f不变
        System.out.println(new String(bytes,0,len3)); // g

        int len4 = fis1.read(bytes);
        System.out.println(len4); // -1
        System.out.println(new String(bytes)); // gef!!!!!!!!!!!!!!!  读不到数据,就没了 g e f不变

        */

        //循环读取
        int len;
        while ((len = fis1.read(bytes)) != -1){
            System.out.println(new String(bytes,0,len));
        }
        //abc
        //def
        //g

        //关闭流释放资源
        fis1.close();
    }
}
